﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DevExpress.Xpf.Docking;
using DevExpress.Xpf.Docking.Base;

using UCOLOR.Helpers;
using UColor.WPF;
using UCOLOR.Model;
namespace UCOLOR.View
{
    /// <summary>
    /// HomeView.xaml 的交互逻辑
    /// </summary>
    public partial class DirView :  UCModule
    {
        ExhibitionGroupItem Category;
        public DirView(ExhibitionGroupItem category)
        {
            InitializeComponent();
            Category = category;
            ShowView(category);
        }

        public DirView(ExhibitionGroupItem category,UColor.WPF.Internal.BaseControlModulesPage baseModules)
        {
            InitializeComponent();
            this.ParentModulePage = baseModules;
            Category = category;
            ShowView(category);
        }

        public DirView(PartProductCode productCode)
        {
            InitializeComponent();
            //productCode.
            //Category = category;
            //ShowView(productCode); 
        }

        public override void EndInit()
        {
            base.EndInit();
            DirListView view = new DirListView();
            //view.data = UCOLOR.Model.DataHelper.getCategorys("HOME");
            //view.DirList.ItemsSource = view.data;
            //MainView view = new MainView();
            ShowModule("DirListView", view);
        }

        private void Tile_Click(object sender, EventArgs e)
        {
           // ShowMoudel("DirView");
        }

        private void Tile_Click_1(object sender, EventArgs e)
        {
            //ShowMoudel("MainView");
        }

        public void InitTopNav(string Parameter)
        {
            Window window = Application.Current.Windows[0];
            System.Data.DataTable dt =  UCOLOR.DAL.Helper.SqliteDataHelper.GetPartProductCodeTab();
            //if (Parameter == null || Parameter.ToString() == "HOME")
            //{
            this.Nav.ColumnDefinitions.Clear();
            AddColumnBtn(this, "HOME","btnHome");
            foreach (System.Data.DataRow row in dt.Rows)
            {
                AddColumnBtn(this, row.ItemArray[1].ToString(),"btn" + row.ItemArray[0].ToString());
            }
            if (Parameter == "")
            {
                SetButtonStyle("HOME");
            }
            else
            {
                SetButtonStyle(Parameter);
            }
        }


        public void AddColumnBtn(DirView view, string Caption, bool bl)
        {
            ColumnDefinition cd = new ColumnDefinition();
            cd.Width = new GridLength(50.00);
            view.Nav.ColumnDefinitions.Add(cd);
            if (bl)
            {
                Button _btn = new Button();
                _btn.Style = Resources["DefaultStyle"] as Style;
                var _seters = _btn.Style.Setters;
                _btn.BorderBrush=null;
                _btn.Background=null;
                _btn.Content = ">>";
                view.Nav.Children.Add(_btn);
                cd.DataContext = _btn;
                _btn.SetValue(Grid.ColumnProperty, view.Nav.ColumnDefinitions.Count - 1);
            }
            //cd.SetValue(
            //view.navGrid.ColumnDefinitions.Add(cd);
            ColumnDefinition cd2 = new ColumnDefinition();
            cd2.Width = new GridLength(100.00, GridUnitType.Auto);

            Button btn = new Button();
            btn.Style = Resources["DefaultStyle"] as Style;
            //btn.Style = Resources["ModuleLinkButton"] as Style;
            btn.Click +=new RoutedEventHandler(Button_Click);
            btn.Content = Caption;
            view.Nav.Children.Add(btn);
            cd2.DataContext = btn;
            view.Nav.ColumnDefinitions.Add(cd2);
            btn.SetValue(Grid.ColumnProperty, view.Nav.ColumnDefinitions.Count - 1);
        }


        public void AddColumnBtn(DirView view, string Caption,Grid Nav, bool bl)
        {

            ColumnDefinition cd = new ColumnDefinition();
            cd.Width = new GridLength(98.00);
            Nav.ColumnDefinitions.Add(cd);
            if (bl)
            {
                Button _btn = new Button();
                _btn.Style = Resources["DefaultStyle"] as Style;
                _btn.Content = ">>";
                Nav.Children.Add(_btn);
                cd.DataContext = _btn;
                _btn.SetValue(Grid.ColumnProperty, Nav.ColumnDefinitions.Count - 1);
            }
            //cd.SetValue(
            //view.navGrid.ColumnDefinitions.Add(cd);
            ColumnDefinition cd2 = new ColumnDefinition();
            cd2.Width = new GridLength(98.00);// GridUnitType.Auto);

            Button btn = new Button();
            //btn.Style = Resources["ModuleLinkButton"] as Style;
            btn.Style = Resources["DefaultStyle"] as Style;
            btn.Click += new RoutedEventHandler(Button_Click);
            btn.Content = Caption;
            Nav.Children.Add(btn);
            cd2.DataContext = btn;
            view.Nav.ColumnDefinitions.Add(cd2);
            btn.SetValue(Grid.ColumnProperty, Nav.ColumnDefinitions.Count - 1);

        }


        public void AddColumnBtn(DirView view,string Caption)
        {
            ColumnDefinition cd = new ColumnDefinition();
            cd.Width = new GridLength(5.00);
            //cd.SetValue()
            view.Nav.ColumnDefinitions.Add(cd);
            ColumnDefinition cd2 = new ColumnDefinition();
            cd2.Width = new GridLength(98.00);//,GridUnitType.Auto);
           
            Button btn = new Button();
            btn.Style = Resources["DefaultStyle"] as Style;
            btn.Background = new ImageBrush(ImageSourceHelper.GetImageSource(AppDomain.CurrentDomain.BaseDirectory+"images/Back.png"));
            //--//--btn.Style--//--   = Resources["ModuleLinkButton"] as Style;
            btn.Click += new RoutedEventHandler(Button_Click);
            btn.Content = Caption;
            view.Nav.Children.Add(btn);
            cd2.DataContext = btn;
            view.Nav.ColumnDefinitions.Add(cd2);
            btn.SetValue(Grid.ColumnProperty, view.Nav.ColumnDefinitions.Count - 1);
        }
        public void AddColumnBtn(DirView view, string Caption,string Uid)
        {
            ColumnDefinition cd2 = new ColumnDefinition();
            cd2.Width = new GridLength(98.00);//,GridUnitType.Auto);
            cd2.Name = Uid;
            Button btn = new Button();
            btn.Uid = Uid;
            btn.Style = Resources["DefaultStyle"] as Style;
            btn.Background = new ImageBrush(ImageSourceHelper.GetImageSource(AppDomain.CurrentDomain.BaseDirectory + "images/Back.png"));
            //--//--btn.Style--//--   = Resources["ModuleLinkButton"] as Style;
            btn.Click += new RoutedEventHandler(Button_Click);
            btn.Content = Caption;
            view.Nav.Children.Add(btn);
            cd2.DataContext = btn;
            view.Nav.ColumnDefinitions.Add(cd2);
            btn.SetValue(Grid.ColumnProperty, view.Nav.ColumnDefinitions.Count - 1);
        }
       
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Button caption = ((Button)sender);
            SetButtonStyle(caption.Content.ToString());
            if (caption.Uid == "btnHome")
            {
                //ShowView(caption.ToString());
                ShowView(null as ExhibitionGroupItem);

            }
            else
            {
                ShowCatalog(((Button)sender).Uid);
            }

        }




        public void ShowView(ModuleView view)
        {
           // ShowModudel("DirListView", view);
        }

        public void SetButtonStyle(string Caption)
        {
            bool IsSub = false;
            if (!IsSub)
            {
                foreach (var el in this.Nav.Children)
                {
                    if (el is Button)
                    {
                        (el as Button).Background = new ImageBrush(ImageSourceHelper.GetImageSource(AppDomain.CurrentDomain.BaseDirectory + "images/Back.png"));
                        if ((el as Button).Content.ToString() == Caption)
                        {
                            (el as Button).Background = new ImageBrush(ImageSourceHelper.GetImageSource(AppDomain.CurrentDomain.BaseDirectory + "images/Back2.png"));
                        }
                    }
                }
            }
        }

        public void ShowView(ExhibitionGroupItem item)
        {
            DirListView view = new DirListView();
            if (item == null || string.IsNullOrEmpty(item.product_code))
            {
                InitTopNav(item==null?"":item.product_code);
                view = new DirListView();
                var data = DataHelper.GetPartProductCode(); 
                view.DirList.ItemsSource = data;
                ShowModule("DirListView", view);
            }
            else
            {
                InitTopNav(item.description);
                //view = new DirListView();
                //var data = DataHelper.GetPartProductCode(item.product_code);
                //view.DirList.ItemsSource = data;
                //ShowModule("DirListView", view);
                ShowCatalog(item.product_code);
            }
        }



        public void ShowModule(string Key, ModuleView view)
        {
            if (this.ViewList[Key] == null)
            {
                this.Center.ColumnDefinitions.Clear();
                this.ViewList.Add(Key, view);
                this.Center.Children.Add(view);
                view.SetValue(Grid.ColumnProperty, 0);
            }
            else
            {
                this.Center.ColumnDefinitions.Clear();
                this.Center.Children.Add(view);
                view.SetValue(Grid.ColumnProperty, 0);
            }
        }

        
        /// <summary>
        /// 显示二级分类
        /// </summary>
        /// <param name="catalog_no"></param>
        public void ShowCatalog(string catalog_no)
        {
            string key = catalog_no.Replace("btn", "");
            TabControl tab;
            if (!tabList.ContainsKey(key))
            {
                tab = new TabControl();
                tab.Uid = "tab"+key;
                tabList.Add(key, tab);
                IList<PartProductFamily> list = DataHelper.GetPartProductFamily(key);
                if (list != null)
                {
                    TabItem allTab = new TabItem();
                    allTab.Header = "全部";
                    tab.Items.Add(allTab);
                    //tab.SelectedIndex = tab.Items.Count - 1;
                    ProductListView allView = new ProductListView();
                    allView.ParentModulePage = this.ParentModulePage;
                    allView.PhoteSources = list;
                    allTab.Content = allView;
                    allTab.Padding = new Thickness(2, 2, 2, 2);
                    foreach (PartProductFamily part in list)
                    {
                        TabItem temptb = new TabItem();
                        temptb.Header = part.product_family;
                        tab.Items.Add(temptb);
                        //tab.SelectedIndex = tab.Items.Count - 1;
                        ProductListView productView = new ProductListView(part.product_family);
                        productView.ParentModulePage = this.ParentModulePage;
                        temptb.Content = productView;
                        temptb.Padding = new Thickness(2, 2, 2, 2);
                    }
                }
            }
            else
            {
                tab = tabList[key] as TabControl;
            }
            this.Center.ColumnDefinitions.Clear();
            int cCount = this.Center.Children.Count;
            for(int i=0 ;i<cCount;i++)
            {
                this.Center.Children.RemoveAt(0);
            }
            //this.ViewList.Add(Key, view);
            tab.Margin = new Thickness(0,3, 0, 0);
            this.Center.Children.Add(tab);
            tab.SetValue(Grid.ColumnProperty, 0);
        }

        public System.Collections.Hashtable ViewList = new System.Collections.Hashtable();
        public System.Collections.Hashtable tabList = new System.Collections.Hashtable();

        private void Tile_Click_2(object sender, EventArgs e)
        {

        }

        private void Tile_Click_3(object sender, EventArgs e)
        {
         //   CartView view = new CartView();
         //   view.CurrentCart.ItemsSource =UCOLOR.Model.DataHelper.getUserCarts("");
         //   ShowModudel("CartView", view);
        }


        private void ShowModalDialog(bool bShow)
        {
            this.ModalDialog.IsOpen = bShow;
            //this.MainPanel.IsEnabled = !bShow;
        }

        private void Dlg_BtnClose_Click(object sender, RoutedEventArgs e)
        {
            ShowModalDialog(false);
        }


        private void Dlg_BtnOK_Click(object sender, RoutedEventArgs e)
        {
            ShowModalDialog(false);
            //labelString.Content = "The string is: " + TxtBoxInput.Text;
        }  

        private void expander1_Expanded(object sender, RoutedEventArgs e)
        {
            ShowModalDialog(((Button)sender).Content.ToString()==">>");
            ((Button)sender).Content = ((Button)sender).Content.ToString() == ">>" ? "<<" : ">>";

        }
    }
}
